﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace h2100
{
    public class hFogOfWar
    {
        private Texture2D _fogofwar;

        public hFogOfWar(ContentManager content)
        {
            _fogofwar = content.Load<Texture2D>(hGlobal.PathTerrain+"Fog");
        }

        public void update(hParams args)
        {

            //for (int i = 0; i < args.Grid.NRows; i++)
            //{
            //    for (int j = 0; j < args.Grid.NColumns; j++)
            //    {
            //        args.MapManager.Map.MapCells[i, j].onFogOfWar = true;
            //    }
            //}

            for (int i = 0; i < args.Grid.NRows; i++)
            {
                for (int j = 0; j < args.Grid.NColumns; j++)
                {
                    if (args.StructureManager.Structures[i, j] != null && args.StructureManager.Structures[i, j].Controler.Equals("player"))
                    {
                        args.MapManager.Map.MapCells[i, j].onFogOfWar = false;
                        if (i - 1 >= 0)
                        {
                            args.MapManager.Map.MapCells[i - 1, j].onFogOfWar = false;
                        }
                        if (i + 1 < args.Grid.NRows)
                        {
                            args.MapManager.Map.MapCells[i + 1, j].onFogOfWar = false;
                        }
                        if (j - 1 >= 0)
                        {
                            args.MapManager.Map.MapCells[i, j - 1].onFogOfWar = false;
                        }
                        if (j + 1 < args.Grid.NColumns)
                        {
                            args.MapManager.Map.MapCells[i, j + 1].onFogOfWar = false;
                        }
                    }
                }
            }
        }

        public void draw(SpriteBatch spriteBatch, hParams args)
        {
            for(int i=0;i<args.MapManager.Map.NRows;i++)
                for (int j = 0; j < args.MapManager.Map.NColumns; j++)
                {
                    if (args.MapManager.Map.MapCells[i, j].onFogOfWar)
                    {
                        spriteBatch.Draw(_fogofwar,new Vector2(args.MapManager.Map.MapCells[i, j].X,args.MapManager.Map.MapCells[i, j].Y),Color.White);
                    }
                }
        }
    }
}
